﻿using Futajma.MigrationStudio.LiveLinkImporter.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Futajma.MigrationStudio.LiveLinkImporter.Tdx
{
    public class Item
    {
        protected XElement ItemElement {get; private set;}
        protected TDX TDX { get; private set; }

        public Item(XElement itemElement, TDX tdx)
        {
            TDX = tdx;
            ItemElement = itemElement;
        }

        protected XElement GetStringProperty(string propertyName)
        {
            //ItemElement.Descendants("property").FirstOrDefault()

            //Console.WriteLine(string.Format("{0}-{1}", propertyName, DateTime.Now.Millisecond));
            var property = ItemElement.Descendants("property").FirstOrDefault(x => (string)x.Attribute("ref") == propertyName);
            //Console.WriteLine(string.Format("{0}-{1}", propertyName, DateTime.Now.Millisecond));

            return property; 
        }

        public XAttribute GetAttribute(string attributeName)
        {
            return ItemElement.Attribute(attributeName);
        }

        public string GetStringAttribute(string attributeName)
        {
            return (string)GetAttribute(attributeName);
        }

        public string LiveLinkId { get { return (string)GetStringProperty("liveLinkId"); } }
        public int FileSize { get { int size = 0; int.TryParse((string)GetStringProperty("fileSize"), out size); return size; } }
        public string Title { get { return System.Web.HttpUtility.UrlDecode((string)GetStringProperty("title")); } }
        public LiveLinkType LiveLinkType { get { return (LiveLinkType)int.Parse((string)GetStringProperty("itemType")); } }



    }
}
